import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"
import { TemplateConfig } from "./model"

enum Api {
  page = "/msg-push-service/msgNotice/page",
  list = "/msg-push-service/msgMotice/listAll",
  save = "/msg-push-service/msgNotice/save",
  edit = "/msg-push-service/msgNotice/update",
  get = "/msg-push-service/msgNotice/get",
  deleteBatch = "/msg-push-service/msgNotice/deleteBatch",
  listChannel = "/msg-push-service/msgNotice/listChannel",

  saveNoticeChannel = "/msg-push-service/noticeChannel/save",
  updateNoticeChannel = "/msg-push-service/noticeChannel/update",
  settingNoticeChannel = "/msg-push-service/noticeChannel/setting",
  getNoticeChannel = "/msg-push-service/noticeChannel/get",

  saveGroup = "/msg-push-service/msgGroup/save",
  updateGroup = "/msg-push-service/msgGroup/update",
  batchDeleteGroup = "/msg-push-service/msgGroup/batchDelete",
  deleteGroup = "/msg-push-service/msgGroup/delete",

  listMsgSubscriber = "/msg-push-service/msgReceiver/listMsgSubscriber",
  saveMsgSubscriber = "/msg-push-service/msgReceiver/saveMsgSubscriber",
  deleteMsgSubscriber = "/msg-push-service/msgReceiver/deleteMsgSubscriber",

  // 消息字段库
  listFieldLibrary = "/msg-push-service/msgFieldLibrary/list",
  saveFieldLibrary = "/msg-push-service/msgFieldLibrary/save",
  updateFieldLibrary = "/msg-push-service/msgFieldLibrary/update",
  deleteFieldLibrary = "/msg-push-service/msgFieldLibrary/delete",
  batchDeleteFieldLibrary = "/msg-push-service/msgFieldLibrary/batchDelete",
}

export const listFieldLibrary = (params) => {
  return defHttp.get({ url: Api.listFieldLibrary, params })
}

export const saveOrUpdateFieldLibrary = (params, isUpdate) => {
  const url = isUpdate ? Api.updateFieldLibrary : Api.saveFieldLibrary
  return defHttp.post({ url: url, params })
}

export const deleteFieldLibrary = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteFieldLibrary, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

export const batchDeleteFieldLibrary = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteFieldLibrary, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

/**
 * 消息订阅者
 */
export const listMsgSubscriber = (params) => defHttp.get({ url: Api.listMsgSubscriber, params })
export const saveMsgSubscriber = (params) => defHttp.post({ url: Api.saveMsgSubscriber, params })
export const deleteMsgSubscriber = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认移除${params.name}`,
    content: "是否移除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteMsgSubscriber, data: { ...params } }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const saveOrUpdateGroup = (params, isUpdate) => {
  const url = isUpdate ? Api.updateGroup : Api.saveGroup
  return defHttp.post({ url: url, params })
}

export const deleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除${params.name}`,
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteGroup, data: { id: params.id } }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const batchDeleteGroup = (params, handleSuccess) => {
  Modal.confirm({
    title: `确认删除`,
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteGroup, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}

export const getNoticeChannel = (params) => {
  return defHttp.get({ url: Api.getNoticeChannel, params })
}

export const saveNoticeChannel = (params) => {
  const url = "/msg-push-service/noticeChannel/save"
  return defHttp.post({ url: url, params })
}

export const settingNoticeChannel = (params) => {
  return defHttp.post({ url: Api.settingNoticeChannel, params })
}

export const pageNotice = (params) => {
  return defHttp.get({ url: Api.page, params })
}

/**
 * 查询全部渠道
 * @param params
 */
export const listChannel = () => {
  return defHttp.get({ url: Api.listChannel, params: {} })
}

export const saveOrUpdateNotice = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save
  return defHttp.post({ url: url, params })
}

export const getNotice = (params) => {
  return defHttp.get({ url: Api.get, params })
}

export const deleteNotice = (params, handleSuccess) => {
  return defHttp.delete({ url: "/msg-push-service/msgNotice/delete", data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

export const batchDeleteNotice = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}
